package com.bkhech.boot.sample.mvc.domain;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author guowm
 * @date 2023/2/1
 */
@Data
public class UserInfo implements UserDetails, Serializable {
    private Long id;
    private String username;
    private String password;
    /**
     * 用户权限
     */
    private String role;
    /**
     * 账号是否过期
     * 过期账号无法验证
     */
    private Boolean expired;
    /**
     * 账号是否被锁定
     * 锁定账号无法验证
     */
    private Boolean locked;
    /**
     * 账号是否可用
     * 禁用账号无法验证
     */
    private Boolean enabled;

    public UserInfo() {

    }

    public UserInfo(Long id, @NotNull String username, @NotNull String password, String role, Boolean expired, Boolean locked, Boolean enabled) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.role = role;
        this.expired = expired;
        this.locked = locked;
        this.enabled = enabled;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(new SimpleGrantedAuthority(this.role));
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return !this.expired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !this.locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
